package com.riven.bilibilidemo.utils;

import com.google.gson.Gson;
import com.riven.bilibilidemo.application.MyApplication;
import com.riven.bilibilidemo.interfaces.RequestJson2BeanCallBack;

import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

import okhttp3.Cache;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.logging.HttpLoggingInterceptor;

/**
 * @author rivenlee
 * @date 2017/2/19 16:09
 */

public class HttpUtil {

    private static HttpUtil httpUtil;
    private static OkHttpClient mOkHttpClient;
    private final Gson gson;
    static {
        initOkHttpClient();
    }

    /**
     * 单例模式
     * @return
     */
    public static HttpUtil newInstance(){
        if(httpUtil == null){
            synchronized (HttpUtil.class){
                if(httpUtil == null){
                    httpUtil = new HttpUtil();
                }
            }
        }
        return  httpUtil;
    }

    private HttpUtil(){
        gson = new Gson();
    }

    public <T> void getRequest(String url , final Class<T> classz, final RequestJson2BeanCallBack<T> callBack){
        Request request = new Request.Builder().url(url).build();
        mOkHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

                callBack.onFail("请求网络失败");
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                final T bean = gson.fromJson(response.body().string(), classz);
                if(bean != null){
                    UIUtils.runOnMainThread(new Runnable() {
                        @Override
                        public void run() {
                            callBack.onSucces(bean);
                        }
                    });
                }
            }
        });
    }

    private static void initOkHttpClient() {

        HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
        interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);

        if (mOkHttpClient == null) {
            synchronized (HttpUtil.class) {
                if (mOkHttpClient == null) {
                    //设置Http缓存
                    Cache cache = new Cache(new File(MyApplication.getContext()
                            .getCacheDir(), "HttpCache"), 1024 * 1024 * 10);
                    mOkHttpClient = new OkHttpClient.Builder()
                            .cache(cache)
                            .addNetworkInterceptor(new CacheInterceptor())
                            .retryOnConnectionFailure(true)
                            .connectTimeout(30, TimeUnit.SECONDS)
                            .writeTimeout(20, TimeUnit.SECONDS)
                            .readTimeout(20, TimeUnit.SECONDS)
                            .build();
                }
            }
        }
    }


}
